<template>
  <div class="app-container">
    <!-- 个人信息 -->
    <el-form ref="fm" :model="userData" :rules="rules" label-width="100px">
      <!-- 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item prop="departmentName" label="部门">
            <el-input
              v-model="userData.departmentName"
              @focus="showTree = true"
            />
            <el-dep-select :visible.sync="showTree" @current-select="getDept" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item prop="timeOfEntry" label="入职时间">
            <el-date-picker
              v-model="userData.timeOfEntry"
              type="date"
              class="inputW"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <!-- 回显已经上传的图片
            能够重新选择上传并可以获取最新上传图片的地址 -->
            <UploadImg :src.sync="userData.staffPhoto">
              <!-- <UploadImg /> -->
              <!-- <el-image :src="require('@/assets/common/head.jpg')"/>  -->
            </UploadImg>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="updateDetail">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { saveUserDetailById } from '@/api/employees'
import UploadImg from '@/components/UploadImg'
export default {
  components: {
    UploadImg
  },
  props: {
    userData: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      showTree: false,
      rules: {
        departmentName: [
          { rquired: true, message: '姓名不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { rquired: true, message: '入职时间不能为空', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 选择部门选择弹层
    getDept (node) {
      console.log(node)
      this.userData.departmentName = node.name
    },
    updateDetail () {
      this.$refs.fm.validate(async (isOk) => {
        if (isOk) {
          await saveUserDetailById(this.userData)
          this.$message.success('更新账户成功')
        }
      })
    }
  }

}
</script>
